//
//  TDMineMessageC.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/21.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDMineMessageC.h"

#import "TDMineMessageHeadCell.h"
#import "TDMineMessageListCell.h"

#import "TDMineMessageListModel.h"
#import "NetWorkRequest.h"
@interface TDMineMessageC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,copy)NSString *totalUnReadNum;

@end

@implementation TDMineMessageC

-(void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //加载数据
    [self loadData];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}



- (IBAction)navBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    
    //添加tableView
    [self setupTableView];
}

- (void)setupNavItem {
    self.titleText = @"消息";
}

- (void)setupTableView {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDMineMessageHeadCell" bundle:nil] forCellReuseIdentifier:@"TDMineMessageHeadCellId"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TDMineMessageListCell" bundle:nil] forCellReuseIdentifier:@"TDMineMessageListCellId"];
    self.tableView.showsVerticalScrollIndicator = NO;
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreNetData];
    }];
}

#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TDMineMessageHeadCellId";
    if (indexPath.row == 0) {
        iden = @"TDMineMessageHeadCellId";
    }
    else {
        iden = @"TDMineMessageListCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        TDMineMessageHeadCell *mineMessageHeadCell = (TDMineMessageHeadCell *)cell;
        mineMessageHeadCell.selectionStyle = UITableViewCellSelectionStyleNone;
        mineMessageHeadCell.lbe_messageNum.text = [NSString stringWithFormat:@"您有%@条未读消息",self.totalUnReadNum];
        return;
    }
    TDMineMessageListCell *mineMessageListCell = (TDMineMessageListCell *)cell;
    TDMineMessageListModel *messageListModel = self.dataArray[indexPath.row];
    mineMessageListCell.messageListModel = messageListModel;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 107;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TDMineMessageListModel *messageListModel = self.dataArray[indexPath.row];
    if ([messageListModel.readflag isEqualToString:@"1"]) {
        return;
    }
    [self readMessageNet:messageListModel];
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.row == 0) {
        return NO;
    }
    return YES;
    
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.row == 0) {
        return UITableViewCellEditingStyleNone;
    }
    return UITableViewCellEditingStyleDelete;
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return;
    }
    if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
        if (indexPath.row<[self.dataArray count]) {
            TDMineMessageListModel *messageListModel = self.dataArray[indexPath.row];
            [self deleteMessageNet:messageListModel indexPath:indexPath];
            
        }
        
    }
}

- (IBAction)readAllMessageBtnClick:(UIButton *)sender {
    [self readMessageNet:nil];
}



// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    NSDictionary *parDic = @{
                             @"lastseqid":@"0",
                             @"pagesize":@"7",
                             };
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:nil];
    [NetWorkRequest postNetRequestData:Messagelist parmeters:parDic response:^(NSDictionary *response) {
        if (response) {
            [weakSelf progressHUDHideImmediately];
            weakSelf.totalUnReadNum = [HelpObject changeNull:response[@"data"][@"unread"]];
            weakSelf.dataArray = [NSArray yy_modelArrayWithClass:[TDMineMessageListModel class] json:response[@"data"][@"messagelist"]].mutableCopy;
            TDMineMessageListModel *tmpModel = [[TDMineMessageListModel alloc] init];
            [weakSelf.dataArray insertObject:tmpModel atIndex:0];
            [weakSelf.tableView reloadData];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:nil isSucceed:NO];
    }];
}


// MARK: - 获取网络数据-----
- (void)loadMoreNetData {
    __weak typeof(self) weakSelf = self;
    NSString *lastseqid = @"";
    if (self.dataArray && self.dataArray.count != 0 ) {
        TDMineMessageListModel *lastModel = self.dataArray.lastObject;
        lastseqid = lastModel.seqid;
    }
    NSDictionary *parDic = @{
                             @"lastseqid":lastseqid,
                             @"pagesize":@"7",
                             };
    
    [self progressHUDShowWithText:nil];
    [NetWorkRequest postNetRequestData:Messagelist parmeters:parDic response:^(NSDictionary *response) {
        [weakSelf.tableView.mj_footer endRefreshing];
        if (response) {
            [weakSelf progressHUDHideImmediately];
            NSArray *tmpArray = [NSArray yy_modelArrayWithClass:[TDMineMessageListModel class] json:response[@"data"][@"messagelist"]].mutableCopy;
            if (!tmpArray || tmpArray.count == 0) {
                weakSelf.noticeText = @"暂无更多数据";
                return;
            }
            [weakSelf.dataArray addObjectsFromArray:tmpArray];
            [weakSelf.tableView reloadData];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf progressHUDShowWithCompleteText:nil isSucceed:NO];
    }];
}


// MARK: - 阅读消息接口-----
- (void)readMessageNet:(TDMineMessageListModel *)selNotiModel {
    NSString *seqid = [HelpObject changeNull:selNotiModel.seqid];
    NSString *oldupdatetime = [HelpObject changeNull:selNotiModel.updatetime];
    NSDictionary *parDic = @{
                             @"seqid":seqid,
                             @"oldupdatetime":oldupdatetime,
                             };
    if (!selNotiModel) {
        parDic = @{
                   @"oldupdatetime":oldupdatetime,
                   };
    }
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:nil];
    [NetWorkRequest postNetRequestData:ReadMessageTD parmeters:parDic response:^(NSDictionary *response) {
        if (response) {
            selNotiModel.readflag = @"1";
            [weakSelf loadNetData];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:nil isSucceed:NO];
    }];
}


// MARK: - 删除消息接口-----
- (void)deleteMessageNet:(TDMineMessageListModel *)selNotiModel indexPath:(NSIndexPath *)indexPath {
    NSString *seqid = [HelpObject changeNull:selNotiModel.seqid];
    NSString *oldupdatetime = [HelpObject changeNull:selNotiModel.updatetime];
    NSDictionary *parDic = @{
                             @"seqid":seqid,
                             @"oldupdatetime":oldupdatetime,
                             @"loginuserid":[AppSetting shareAppSetting].memberid
                             };
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:nil];
    [NetWorkRequest postNetRequestData:DeleteMessageTD parmeters:parDic response:^(NSDictionary *response) {
        if (response) {
            [weakSelf.dataArray removeObjectAtIndex:indexPath.row];//移除数据源的数据
            [weakSelf.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的数据
            [weakSelf loadNetData];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:nil isSucceed:NO];
    }];
}


@end
